import { Component, EventEmitter, Injector, Output, ViewChild, Input, OnInit } from '@angular/core';
import { AppComponentBase } from '@shared/common/app-component-base';
import { ModalDirective } from 'ngx-bootstrap';

import { finalize } from 'rxjs/operators';
import * as _ from 'lodash';
import { CodeItemServiceProxy } from '@shared/service-proxies/stock-service-proxies';
import {
    StockZT_BaseInfoServiceProxy,
    EditDtoExtensionsItem,
    Stock_BIMarketEditDto
} from '@shared/service-proxies/stock-service-proxies';
@Component({
    selector: 'createOrEditMarkBaseInfoModal',
    templateUrl: './create-or-edit-mark-baseinfo-modal.component.html'
})
export class CreateOrEditMarkBaseInfoModalComponent extends AppComponentBase implements OnInit {
    constructor(
        injector: Injector,
        private  _BaseInfoServiceProxy: StockZT_BaseInfoServiceProxy,
        private _codeItemServiceProxy: CodeItemServiceProxy
    ) {
        super(injector);
    }

    @ViewChild('createOrEditModal') modal: ModalDirective;

    @Output() modalSave: EventEmitter<any> = new EventEmitter<any>();
    @Input() stocktype;
    active = false;
    saving = false;
    _type;
    selectedValue = new Stock_BIMarketEditDto();
    editDto;
    extensions: Array<EditDtoExtensionsItem>;
    extendextensions: Array<EditDtoExtensionsItem>;
    optionAll;
    itemcode = '';
    ngOnInit(): void {}
    showinfo(id?: string, codeItemId?: string, codeName?: string, codeItemcode?: string): void {
        if ((id == null || id == undefined) && (codeItemId == undefined || codeItemId == null)) {
            this.notify.error(this.l('请选择市采物资编码分类！'));
            return;
        }
        debugger;
        this._codeItemServiceProxy.checkIsLastTree(codeItemId).subscribe(checkresult => {
            if (checkresult == false && id == undefined) {
                debugger;
                this.notify.error(this.l('请选择到最后一层物资编码再新增！'));
                return;
            }
            this._BaseInfoServiceProxy.getMarketDtoExtensionsById(id, 'YZ').subscribe(result => {
                this.optionAll = {
                    f_DimensionGroupIDOptions: result.dimentionSelectOptions,
                    f_UnitIdOptions: result.unitIdSelectOptions
                };
                this.editDto = result.stock_BIMarketEditDto;
                this.extensions = result.baseItems;
                this.extendextensions = result.extendItems;
                if (id == undefined) {
                    this._codeItemServiceProxy.getCodeItemFatherTree(codeItemId).subscribe(result => {
                        this.editDto.f_CAL_ZeroLevel = result.items[1].code;
                        this.editDto.f_CAL_ZeroLevelName = result.items[1].displayName;
                        this.editDto.f_CAL_FirstLevel = result.items[2].code;
                        this.editDto.f_CAL_FirstLevelName = result.items[2].displayName;
                        this.editDto.f_CAL_SecondLevel = result.items[3].code;
                        this.editDto.f_CAL_SecondLevelName = result.items[3].displayName;
                        this.editDto.f_CAL_ThirdLevel = result.items[4].code;
                        this.editDto.f_CAL_ThirdLevelName = result.items[4].displayName;
                        this.editDto.f_CAL_ForthLevel = result.items[5].code;
                        this.editDto.f_CAL_ForthLevelName = result.items[5].displayName;
                        this.editDto.f_CAL_FifthLevel = result.items[6].code;
                        this.editDto.f_CAL_FifthLevelName = result.items[6].displayName;
                        this.editDto.f_CCAL_ItemClassName = codeName;
                        this.editDto.f_CAL_ItemClassCode = result.items[6].code;
                    });
                }
            });

            this.active = true;
            this.modal.show();
        });
    }

    save(): void {
        this.saving = true;
        let t: object = _.filter(this.optionAll.f_DimensionGroupIDOptions, {
            value: this.editDto.f_DimensionGroupID
        });

        this.editDto.f_DimensionGroup = t[0].label;
        this._BaseInfoServiceProxy
            .createOrUpdateBIMarket(this.editDto)
            .pipe(
                finalize(() => {
                    this.saving = false;
                })
            )
            .subscribe(() => {
                this.notify.info(this.l('SavedSuccessfully'));
                this.close();
                this.modalSave.emit(null);
            });
    }

    close(): void {
        this.active = false;
        this.modal.hide();
    }
}
